from aiogram import types
from aiogram_dialog import DialogManager
from fluentogram import TranslatorRunner

from src.application.user.service import UserService
from src.domain.user.value_objects.user_email import WrongEmailFormat
from src.presentation.tg_bot.states import ClientSG


async def register_user(message: types.Message, _, manager: DialogManager, email: str):
    locale: TranslatorRunner = manager.middleware_data.get("locale")
    user_service: UserService = manager.middleware_data.get("user_service")

    try:
        await user_service.register_user(message.from_user.id, email)
    except WrongEmailFormat:
        await message.answer(locale.get("wrong-email"))
        return

    await message.answer(locale.get("register-success"))

    await manager.switch_to(ClientSG.start)
